import urllib.request
#设置URL
url='https://movie.douban.com'
#设置自定义请求头,内容来自movie.douban.com的F12->Network->XHR->Headers
headers={
        'User-Agent': 'Mozilla/5.0 (X11; CrOS armv7l 13597.84.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.187 Safari/537.36',
        'Referer':'https://movie.douban.com/',
        'Connection':'keep-alive'
        }
#设置request的请求头
req=urllib.request.Request(url,headers=headers)
#使用urlopen打开req
response=urllib.request.urlopen(req,None,10)
#读取返回的内容
html=response.read().decode('utf-8')
#写入返回的内容
f=open("doubanMovie.txt","w",encoding="utf-8")
f.write(html)
f.close()
